package com.dubbo.client;

import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.net.Socket;

public class MyInvocation<T> implements InvocationHandler{

	private String host;

	private int port;
	
	
	public MyInvocation(String host, int port) {
		super();

		this.host = host;
		this.port = port;
	}




	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

		Socket socket=new Socket(host,port);

		//发送数据
		ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
		oos.writeObject(args);//传参数
		oos.writeUTF("com.dubbo.test.MyImpl");//接口实现类的全限定名
		oos.writeUTF(method.getName());//方法的名称
		oos.writeObject(method.getParameterTypes());//参数的类型

		//接收返回的数据
		ObjectInputStream ois=new ObjectInputStream(socket.getInputStream());
		Object result=ois.readObject();

		socket.close();
		    
		return result;
	}
	
	
	

}
